﻿using FashionWebsite.Models;
using FashionWebsite.Models.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FashionWebsite.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        ShopOnlineDataContext data = new ShopOnlineDataContext();
        private DalProduct getProduct = new DalProduct();
        public static int stylepage =1 ;
        public const int PageSize = 12;

        public ActionResult ProductByStatus(int?id)
        {
            var persons = DalProduct.GetProductBy(id);
            var people = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = persons.ToList();
            people.Data = ctx.product.OrderBy(p => p.ProductId).Take(PageSize).ToList();
            people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));

            return View(people);
        }
        public ActionResult ProductByCategory(int? id)
        {
            var productByCategory = DalProduct.GetProductsByCaregory(id);
            var pageProduct = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = productByCategory.ToList();
            pageProduct.Data = ctx.product.OrderBy(p => p.ProductId).Take(PageSize).ToList();
            pageProduct.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));

            return View(pageProduct);
        }
        public ActionResult ListProductByCategory(int? id, int page)
        {

            var productByCategory = DalProduct.GetProductsByCaregory(id);
            var pageProduct = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = productByCategory.ToList();
            pageProduct.Data = productByCategory.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
            pageProduct.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));
            pageProduct.CurrentPage = page;


            return View(pageProduct);

        }
        public ActionResult Index()
        {

            var persons = from c in data.Products select c;
            var people = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = persons.ToList();
            people.Data = ctx.product.OrderBy(p => p.ProductId).Take(PageSize).ToList();
            people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));

            return View(people);
        }
        public ActionResult ProductByBy(int? id)
        {
            
            if (Request.Form["Page"] == null)
            {
                var persons = DalProduct.GetProductBy(1);
                var people = new page<Product>();

                var ctx = new DalProduct();
                ctx.product = persons.ToList();
                people.Data = ctx.product.OrderBy(p => p.ProductId).OrderByDescending(p => p.ProductId).Take(PageSize).ToList();
                people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));

                return View(people);
            }
            else
            {

                stylepage = int.Parse(Request.Form["Page"]);
                var persons = DalProduct.GetProductBy(stylepage);
                var people = new page<Product>();

                var ctx = new DalProduct();
                ctx.product = persons.ToList();
                people.Data = DalProduct.GetProductBy(stylepage).Take(PageSize).ToList(); //ctx.product.OrderBy(p => p.ProductId).OrderByDescending(p => p.ProductId).Take(PageSize).ToList();
                people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));

                return View(people);
            }
        }
        
        
        public ActionResult ListProductByBy(int? id, int page)
        {
            
                var persons = DalProduct.GetProductBy(stylepage);
                var people = new page<Product>();

                var ctx = new DalProduct();
                ctx.product = persons.ToList();
                people.Data = persons.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));
                people.CurrentPage = page;


                return View(people);
          
        }
        public ActionResult PersonList(int? id ,int page)
        {
            var persons = DalProduct.GetProductBy(id);
            var people = new page<Product>();

            var ctx = new DalProduct();
            ctx.product = persons.ToList();
            people.Data = ctx.product.OrderBy(p => p.ProductId).Skip(PageSize * (page - 1)).Take(PageSize).ToList();
            people.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)ctx.product.Count() / PageSize));
            people.CurrentPage = page;


            return View(people);
        }

    }
}
